package cn.yili12.server.client.handler;

import cn.yili12.common.util.IdTool;
import cn.yili12.server.client.vo.ClientMessage;
import cn.yili12.server.info.vo.InfoMessage;
import cn.yili12.server.info.vo.InfoMessageType;
import com.alibaba.fastjson.JSON;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SendClientHandler extends BaseClientHandler {
    @Autowired
    private IdTool idTool;

    @Override
    public void handler(ClientMessage message) {
        //生成唯一标识
        message.setNo(String.valueOf(idTool.createId()));
        //广播到所有消息服务器
        InfoMessage infoMessage = toInfoMessage(message.getData(), message.getFrom(), message.getTo(), message.getNo(), InfoMessageType.Receive);
        producer.send(properties.getInfoQueue(), JSON.toJSONString(infoMessage));
        //返回客户端
        channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(message)));
        //广播到数据服务器进行保存
        producer.send(properties.getDbQueue(), JSON.toJSONString(message));
    }
}
